package com.atguigu.tingshu.search.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.search.service.MqOpsService;
import com.atguigu.tingshu.search.strategy.ProcessDiffTypePurchases;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class MqOpsServiceImpl implements MqOpsService {
    @Autowired
    private ItemService itemService;
    @Autowired
    private Map<String, ProcessDiffTypePurchases> map;

    @Override
    public void listenOnSaleAlbum(String albumId) {
        itemService.onAlbumEs(Long.parseLong(albumId));
    }

    @Override
    public void listenOffSaleAlbum(String albumId) {
        itemService.offAlbumEs(Long.parseLong(albumId));
    }

    @Override
    public void updateAlbumStat(String orderInfoStr) {
        OrderInfo orderInfo = JSONObject.parseObject(orderInfoStr, OrderInfo.class);
        //采用策略模式执行对应类型的方法
        map.get(orderInfo.getItemType()).processTypePurchases(orderInfo);
    }
}
